<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);
include_once($phpbb_root_path . 'ofc/php-ofc-library/open-flash-chart.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];

if ($user_id == ANONYMOUS)
{
	login_box();
}
if ($auth->acl_getf_global('m_') == false)
{
	// no enough priviledge, return.
	login_box();
}

// Basic parameter data
$mode		= request_var('mode', '');

// Stop a poll
if ($mode == 'stop_poll')
{
	// stop a poll by setting the expiring time very short.
	$stop_poll_topic_id = (int)(request_var('stop_poll_topic_id', ''));
	
	$sql2 = 'UPDATE ' . TOPICS_TABLE . ' SET poll_length=1 WHERE topic_id=' . $stop_poll_topic_id;
	$result2 = $db->sql_query($sql2);
}
else if ($mode == 'resume_poll')
{
	// resume a poll by setting the poll_length to 0.
	$resume_poll_topic_id = (int)(request_var('resume_poll_topic_id', ''));
	
	$sql2 = 'UPDATE ' . TOPICS_TABLE . ' SET poll_length=0 WHERE topic_id=' . $resume_poll_topic_id;
	$result2 = $db->sql_query($sql2);
}


// Get all polls
$sql = 'SELECT * FROM ' . POLL_OPTIONS_TABLE . 
		" ORDER BY topic_id AND poll_option_id";
$result = $db->sql_query($sql);

$poll_info = array();
while ($row = $db->sql_fetchrow($result))
{
	// Check if the poll is a valid questionnaire owned by the admin.
	// get the topic
	$valid = false;
	$sql2 = 'SELECT * FROM ' . TOPICS_TABLE . ' WHERE topic_id=' . $row['topic_id'];
	$result2 = $db->sql_query_limit($sql2, 1);
	if ($row2 = $db->sql_fetchrow($result2))
	{
		$topic_type = $row2['topic_type'];	
		$forum_id = $row2['forum_id'];
		
		// get the forum
		$sql2 = 'SELECT * FROM ' . FORUMS_TABLE . ' WHERE forum_id=' . $forum_id;
		$result2 = $db->sql_query_limit($sql2, 1);
		if ($row2 = $db->sql_fetchrow($result2))
		{
			$parent_id = $row2['parent_id'];
			
			if ($topic_type == POST_ANNOUNCE && $parent_id == 0)
				$valid = true;			
		}
	}
	
	if ($valid)
		$poll_info[] = $row;		
}
$db->sql_freeresult($result);

// List all polls
$cur_topic_id = 0;

foreach ($poll_info as $poll_option)
{
	if ($poll_option['topic_id'] != $cur_topic_id)
	{
		// a new poll.		
		$cur_topic_id 	= $poll_option['topic_id'];
		$poll_title		= '';		
		$poll_ended		= false;
		
		// get poll text
		$sql = 'SELECT * FROM ' . TOPICS_TABLE . " WHERE topic_id=$cur_topic_id";
		$result = $db->sql_query_limit($sql, 1);

		if ($row = $db->sql_fetchrow($result))
		{
			$poll_title = $row['poll_title'];
			
			if ($row['poll_length'] == 0)
				$poll_ended = false;
			else if ($row['poll_length'] + $row['poll_start'] > time())
				$poll_ended = false;
			else
				$poll_ended = true;				
		}		
	
		$template->assign_block_vars('poll_list', array(
			'TOPIC_ID'						=> $cur_topic_id,
			'POLL_TITLE'					=> $poll_title,
			'POLL_ENDED'					=> $poll_ended,
			)
		);
		
	}
				
	$template->assign_block_vars('poll_list.poll_option_list', array(
		'POLL_OPTION_ID'				=> $poll_option['poll_option_id'],
		'POLL_OPTION_TEXT'				=> $poll_option['poll_option_text'],
		'POLL_OPTION_TOTAL'				=> $poll_option['poll_option_total'],
		)
	);
	
	// List the users voting this option
	$sql = 'SELECT * FROM ' . POLL_VOTES_TABLE . '
			WHERE topic_id = ' . $cur_topic_id . '
				AND poll_option_id = ' . $poll_option['poll_option_id'];
	$result = $db->sql_query($sql);

	while ($row = $db->sql_fetchrow($result))
	{
		$template->assign_block_vars('poll_list.poll_option_list.user', array(
		'USER_NAME'				=> get_username($row['vote_user_id']),
			)
		);
	}
	
}


display_forums('', $config['load_moderators']);

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'U_MONEY'					=> number_format($money),
	
	'S_STOP_POLL'				=> append_sid("{$phpbb_root_path}manage_poll.$phpEx", 'mode=stop_poll'),
	'S_RESUME_POLL'				=> append_sid("{$phpbb_root_path}manage_poll.$phpEx", 'mode=resume_poll'),
	
	
	'S_HOLDER_LIST'				=> $holder_list,
	'S_TRANSACTION_LIST'		=> $transaction_list,
	'S_QUERY_USER'				=> append_sid("{$phpbb_root_path}query_user.$phpEx", 'mode=query_user'),
	'S_CORP_ID'					=> $query_corp_id,
	
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '')
);

// Output page
page_header('Manage Poll');

$template->set_filenames(array(
	'body' => 'manage_poll.html')
);

page_footer();

?>